package com.jie.server.modules.platform.conreoller;

import com.jie.common.annotation.Log;
import com.jie.common.controller.BaseController;
import com.jie.common.enums.BusinessType;
import com.jie.common.page.TableDataInfo;
import com.jie.common.result.R;
import com.jie.server.modules.platform.entity.Tenant;
import com.jie.server.modules.platform.service.ITenantService;
import com.jie.server.modules.platform.vo.TenantResultVo;
import com.jie.server.utils.ExcelUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 租户表 前端控制层
 * @author: xiaojie
 * @since: 2024/12/17 0017
 */
@Api(tags = "租户管理")
@RestController
@RequestMapping("/platform/tenant")
public class TenantController extends BaseController {

    @Autowired
    private ITenantService tenantService;

    /**
     * 查询租户表列表
     */
    @PreAuthorize("@customSs.hasPermi('platform:tenant:list')")
    @GetMapping("/list")
    public TableDataInfo list(Tenant tenant) {
        startPage();
        List<TenantResultVo> list = tenantService.selectTenantList(tenant);
        return getDataTable(list);
    }

    /**
     * 导出租户表列表
     */
    @PreAuthorize("@customSs.hasPermi('platform:tenant:export')")
    @Log(title = "租户表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tenant tenant) {
        List<TenantResultVo> list = tenantService.selectTenantList(tenant);
        ExcelUtils.exportEasyExcel(response,Tenant.class,list,"租户表数据");
    }

    /**
     * 获取租户表详细信息
     */
    @PreAuthorize("@customSs.hasPermi('platform:tenant:query')")
    @GetMapping(value = "/{tenantId}")
    public R getInfo(@PathVariable("tenantId") Long tenantId) {
        return R.ok(tenantService.selectTenantByTenantId(tenantId));
    }

    /**
     * 新增租户表
     */
    @PreAuthorize("@customSs.hasPermi('platform:tenant:create')")
    @Log(title = "租户表", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@RequestBody Tenant tenant) {
        return tenantService.addTenant(tenant);
    }

    /**
     * 修改租户表
     */
    @PreAuthorize("@customSs.hasPermi('platform:tenant:edit')")
    @Log(title = "租户表", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{tenantId}")
    public R update(@PathVariable Long tenantId,  @RequestBody Tenant tenant) {
        tenant.setTenantId(tenantId);
        return tenantService.updateTenant(tenant);
    }

    /**
     * 删除租户表
     */
    @PreAuthorize("@customSs.hasPermi('platform:tenant:remove')")
    @Log(title = "租户表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tenantIds}")
    public R remove(@PathVariable Long[] tenantIds) {
        return R.ok(tenantService.deleteTenantByTenantIds(tenantIds));
    }

}
